package ATMAgain;

import ATM.MyJText;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private ATM.MyJText inCount = new ATM.MyJText("用户名",130,60,this);
    private ATM.MyJText inPwd = new MyJText("密码",130,110,this);

    private int count = 0;

    ICodeService service = new CodeServiceImpl();

    public LoginFrame(){


        this.setLayout(null);

        this.init();

        this.setSize(500,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    }

    private void init(){
        MyButton LoginButton = new MyButton("登录",350,200,this);
        LoginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                login();
            }
        });
    }

    private void login(){
        boolean isRight = service.login(inCount.getText(),inPwd.getText());
        if (!isRight){
            JOptionPane.showMessageDialog(null,"账号或密码有误，重新输入");
            count++;
        }
        JOptionPane.showMessageDialog(null,"登录成功");
        this.dispose();
        new MainFrame();

        if (count >= 3){
            JOptionPane.showMessageDialog(null,"非法用户");
            System.exit(0);
        }
    }


    public static void main(String[] args) {
        new LoginFrame();
    }
}
